package com.thread;

public class DeadLock {

	public static String resource_A = "A";
	public static String resource_B = "B";
	public static void main(String[] args) {
		System.out.println(Thread.currentThread());
		// deadLock();
		// sleepThread 睡眠 1000ms
		final Thread sleepThread = new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				super.run();
			}
		};
		//busyThread一直执行死循环
		Thread busyThread = new Thread() {
			@Override
			public void run() {
				while (true) {

				}
			}
		};
		sleepThread.start();
		busyThread.start();
		sleepThread.interrupt();
		busyThread.interrupt();
		while (sleepThread.isInterrupted()) ;
		System.out.println("sleepThread isInterrupted: " + sleepThread.isInterrupted());
		System.out.println("busyThread isInterrupted: " + busyThread.isInterrupted());
	}

	public static void deadLock() {
		Thread threadA = new Thread(new Runnable() {
			public void run() {
				synchronized (resource_A) {
					System.out.println("get resource A");
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					synchronized (resource_B) {
						System.out.println("get resource B");
					}
				}
			}
		});

		Thread threadB = new Thread(new Runnable() {
			public void run() {
				synchronized (resource_B) {
					System.out.println("get resource B");
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					synchronized (resource_A) {
						System.out.println("get resource A");
					}
				}
			}
		});

		threadA.start();
		threadA.interrupt();
		threadB.start();
	}
}
